package com.wawu.server.dao;

import com.wawu.common.annotation.IOC.component.Repository;
import com.wawu.common.utils.SQLBuilder.SelectSQLBuilder;
import com.wawu.common.utils.SQLExcutorUtil;
import com.wawu.pojo.entity.Good;
import com.wawu.pojo.entity.User;

import java.sql.SQLException;
import java.util.List;

@Repository
public class UsersDAO {
    /**
     * 条件查询用户(现在只是用邮箱查)
     * @param userQuery
     * @return
     */
    public User getUser(User userQuery) throws SQLException {
        SelectSQLBuilder selectSqlBuilder =new SelectSQLBuilder("users")
                .addDynamicCondition("id","=",userQuery.getId())
                .addDynamicCondition("email","=", userQuery.getEmail())
                .addDynamicCondition("nickname","=",userQuery.getNickname());
        User user = SQLExcutorUtil.querySingle(selectSqlBuilder.getSQL(), User.class, selectSqlBuilder.getParams());
        return user;
    }

    /**
     * 条件查询用户(现在只是用邮箱查)
     * @param userQuery
     * @return
     */
    public List<User> getUsers(User userQuery) throws SQLException {
        SelectSQLBuilder selectSqlBuilder =new SelectSQLBuilder("users")
                .addDynamicCondition("email","=", userQuery.getEmail())
                .addDynamicCondition("nickname","=",userQuery.getNickname());
        List<User> users = SQLExcutorUtil.queryList(selectSqlBuilder.getSQL(), User.class, selectSqlBuilder.getParams());
        return users;
    }


    //获取nickname
    public String getNickNameById(Long Id) throws SQLException {
        String sql="SELECT nickname FROM users WHERE id = ? LIMIT 1";
        return SQLExcutorUtil.queryScalar(sql,String.class,Id);
    }

    // token 获取id
    public Integer getIdByToken(String token) throws SQLException {
        String sql="SELECT id FROM users WHERE token = ? LIMIT 1";
        return SQLExcutorUtil.queryScalar(sql,Integer.class,token);
    }


    public User getUserById(Long Id) throws SQLException {
        SelectSQLBuilder sqlBuilder=new SelectSQLBuilder("users")
                .addDynamicCondition("id","=",Id);
        return SQLExcutorUtil.querySingle(sqlBuilder.getSQL(), User.class, sqlBuilder.getParams());
    }
    public List<User> getData(User userQuery) throws SQLException{
        SelectSQLBuilder selectSqlBuilder =new SelectSQLBuilder("users")
                .addDynamicCondition("id","=",userQuery.getId())
                .addDynamicCondition("email","=",userQuery.getEmail())
                .addDynamicCondition("nickname","=",userQuery.getNickname())
                .addDynamicCondition("recipient","=",userQuery.getRecipient())
                .addDynamicCondition("address","=",userQuery.getAddress())
                .addDynamicCondition("phone","=",userQuery.getPhone())
                .addDynamicCondition("headimg","=",userQuery.getHeadimg());
        return SQLExcutorUtil.queryList(selectSqlBuilder.getSQL(), User.class, selectSqlBuilder.getParams());
    }
    /**
     * 插入用户信息
     * @param user
     * @return
     */
    public Long insert(User user) throws SQLException {
        String sql="INSERT INTO `users` (`id`,`email`,`pwd`,`nickname`,`sex`,`recipient`,`address`,`phone`,`headimg`,`updatetime`,`createtime`) VALUES (DEFAULT,?,?,?,0,?,?,?,DEFAULT,?,?);";
        return SQLExcutorUtil.updateAndGetGeneratedKey(sql,user.getEmail(),user.getPwd(),user.getNickname(),user.getRecipient(),user.getAddress(),user.getPhone(),user.getUpdatetime(),user.getCreatetime());

    }


    public static void main(String[] args) throws SQLException {
        UsersDAO usersDAO=new UsersDAO();
        User userQuery=new User();
        userQuery.setEmail("2790440179");

        User user = usersDAO.getUser(userQuery);
        System.out.println(user);
    }
}
